﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MakeItWeb.Framework;
using System.Data;

namespace AllNations.UI
{
    public partial class Blogger : MakeItWebPage, IDynamicPage
    {
        Framework.BloggerConfiguration _Config = new Framework.BloggerConfiguration();
        DataTable _Output;
        bool _OlderPostAvailable = false;

        private int PageSize
        {
            get { return _Config.PageSize; }
        }

        public int PageNum
        {
            get
            {
                if (ViewState["PageNum"] == null)
                    ViewState["PageNum"] = 1;

                return (int)ViewState["PageNum"];
            }
            set
            {
                ViewState["PageNum"] = value;
            }
        }

        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            _Config.Deserialize(CurrentHtmlPage.PageSettings);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            _Output = GetPageData();
        }

        protected void btDeleteBlog_Click(object sender, EventArgs e)
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pBlogId"] = new Guid((sender as LinkButton).CommandArgument);
            GenericCud.ExecuteCud("proc_cm_DeleteBlogEntry", args);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            ClientScript.RegisterStartupScript(GetType(), "CurrentHtmlPageId", string.Format(@"function GetCurrentHtmlPageId() {{ return ""{0}""; }}", CurrentHtmlPage.HtmlPageId), true);
            rptBloggerEntries.DataSource = _Output;
            rptBloggerEntries.DataBind();
        }

        protected void btNewerPost_PreRender(object sender, EventArgs e)
        {
            (sender as LinkButton).Visible = PageNum > 1;
        }

        protected void btOlderPost_PreRender(object sender, EventArgs e)
        {
            (sender as LinkButton).Visible = _OlderPostAvailable;
        }

        protected void btNewerPost_Click(object sender, EventArgs e)
        {
            if (PageNum > 1)
                PageNum--;

            _Output = GetPageData();
        }

        protected void btOlderPost_Click(object sender, EventArgs e)
        {
            if (_OlderPostAvailable)
                PageNum++;

            _Output = GetPageData();
        }

        private DataTable GetPageData()
        {
            DataTable dtOut = null;

            StoredProcArgs args = new StoredProcArgs();
            args["pHtmlPageId"] = CurrentHtmlPage.HtmlPageId;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_ListBlogEntry", args))
            {
                dtOut = ds.Tables[0].Clone();

                int iPageStart = (PageNum - 1) * PageSize;
                int iPageEnd = iPageStart + PageSize;
                for (int idx = iPageStart; idx < iPageEnd && ds.Tables[0].Rows.Count > idx; idx++)
                    dtOut.Rows.Add(ds.Tables[0].Rows[idx].ItemArray);

                _OlderPostAvailable = ds.Tables[0].Rows.Count > iPageEnd;
            }

            return dtOut;
        }

        string IDynamicPage.GetPageConfigurationControl()
        {
            return "~/UI/Controls/BlogConfiguration.ascx";
        }

        string IDynamicPage.PageTypeName
        {
            get { return "Blogger"; }
        }
    }
}